<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 9:05
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Member extends Base {

    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($phone) && $phone != ''){
                $map['a.user_phone|a.user_id'] = ['like', '%' . $phone . '%'];
            }
            if(isset($user_topic)&&$user_topic!="")
            {
                $map['a.user_topic'] = $user_topic;
            }
            if(isset($user_role)&&$user_role!="")
            {
                $map['a.user_role'] = $user_role;
            }

            if(isset($user_estate)&&$user_estate!="")
            {
                $map['a.user_estate'] = $user_estate;
            }

            //注册时间
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.user_time'] = ['between time',[$start,$end]];
            }
            //登录时间
            if(isset($start1)&&$start1!=""&&isset($end1)&&$end1=="")
            {
                $map['a.user_last_time'] = ['>= time',$start1];
            }
            if(isset($end1)&&$end1!=""&&isset($start1)&&$start1=="")
            {
                $map['a.user_last_time'] = ['<= time',$end1];
            }
            if(isset($start1)&&$start1!=""&&isset($end1)&&$end1!="")
            {
                $map['a.user_last_time'] = ['between time',[$start1,$end1]];
            }
            $map['user_status'] =array('neq',2);
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->alias('a')
                ->where($map)->count();// 获取总条数
            $count2 = Db::name('u_user')->alias('a')
                ->where($map)->column('user_id');// 获取总条数
            $lists = Db::name('u_user')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('user_time desc')
                ->select();
//            foreach ($lists as $key=>&$vo){
//                if($vo['user_status']==1){
//                    $vo['user_status_name'] = '正常';
//                }else if($vo['user_status']==3){
//                    $vo['user_status_name'] = '冻结';
//                }
//            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *解冻
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *冻结
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',3);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     *设置话题管理员
     */
    public function up1(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_topic',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *取消设置话题管理员
     */
    public function down1(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_topic',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *取消设置话题管理员
     */
    public function password(){
        if(Request::instance()->isPost()){
            $task_id = input('param.id');
            $password = md5(trim(input('param.password'),' '));
            $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_password',$password);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        $id = input('param.user_id');
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function outExcel(){

        $user_ids = input('ids');
        if($user_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的用户']);
        }
        $lists = Db::name('u_user')
            ->field('user_id, user_phone, user_username, user_role, user_topic, user_estate, user_time, 
            user_last_time')
            ->where('user_id', 'IN', $user_ids)
            ->order('user_time desc')
            ->select();
        foreach($lists as $k => $v){
            if($v['user_role'] == 1){
                $lists[$k]['user_role'] = '租户';
            }elseif ($v['user_role'] == 2){
                $lists[$k]['user_role'] = '房主';
            }elseif ($v['user_role'] == 3){
                $lists[$k]['user_role'] = '未知';
            }

            if($v['user_topic'] == 1){
                $lists[$k]['user_topic'] = '否';
            }elseif ($v['user_topic'] == 2){
                $lists[$k]['user_topic'] = '是';
            }

            if($v['user_estate'] == 1){
                $lists[$k]['user_estate'] = '否';
            }elseif ($v['user_estate'] == 2){
                $lists[$k]['user_estate'] = '是';
            }
            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
        }
        $cellname = [
            ['user_id','ID',20,'LEFT'],
            ['user_phone','手机号',20,'LEFT'],
            ['user_username','昵称',20,'LEFT'],
            ['user_role','房主/租户',20,'LEFT'],
            ['user_topic','是否为话题管理员',20,'LEFT'],
            ['user_estate','是否为物业管理员',20,'LEFT'],
            ['user_time','注册时间',20,'LEFT'],
            ['user_last_time','登录时间',20,'LEFT']
        ];
        $res = exportExcel('预约看房列表', 'Looks', $cellname, $lists);
        return json($res);
    }
}